package com.isyscore.dynomicDemo.controller;

import com.isyscore.dynomicDemo.model.Record;
import com.isyscore.dynomicDemo.model.Result;
import com.isyscore.os.dev.api.common.model.builder.SendMailRequestBuilder;
import com.isyscore.os.dev.api.common.model.builder.SendSmsRequestBuilder;
import com.isyscore.os.dev.api.common.model.domain.MailRequestDomain;
import com.isyscore.os.dev.api.common.model.domain.SmsRequestDomain;
import com.isyscore.os.dev.api.common.service.CommonService;
import com.isyscore.os.dev.util.IsyscoreHashMap;
import com.isyscore.os.dynomicform.model.builder.AddOrUpdateFormDataBuilder;
import com.isyscore.os.dynomicform.model.builder.FindFormDataListBuilder;
import com.isyscore.os.dynomicform.model.domain.*;
import com.isyscore.os.dynomicform.model.result.AddOrUpdateFormDataResult;
import com.isyscore.os.dynomicform.model.result.FindFormDataListResult;
import com.isyscore.os.dynomicform.service.FormBizDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.*;

@RestController
@RequestMapping("/api/app/dynamicdemo")
public class DemoController {

    @Autowired
    private FormBizDataService formBizDataService;

    @Autowired
    private CommonService commonService;

    /**
     *  发送手机
     * @param phoneNumber  发送的手机号码
     * @param token token
     */
    public void sendMessage(String phoneNumber,String token){
        SendSmsRequestBuilder builder=new SendSmsRequestBuilder();
        IsyscoreHashMap headers = new IsyscoreHashMap();
        headers.put("X-Isyscore-Permission-Sid", token);
        builder.setHeaders(headers);
        SmsRequestDomain smsRequestDomain=new SmsRequestDomain();
        smsRequestDomain.setAccessKeyId("123");//短信调用方识别码
        smsRequestDomain.setRequestId("123");//请求id
        smsRequestDomain.setPhoneNumbers(phoneNumber);//要收到短信的手机
        smsRequestDomain.setSign("指令集智能科技");//签名
        smsRequestDomain.setTemplateCode("SMS_166371590");//模板代码
        smsRequestDomain.setTemplateParam("\"code\":123456");//模板代码对应的参数map
        builder.setBizModel(smsRequestDomain);
        commonService.sendSms(builder);
    }

    /**
     * 发送邮件
     */
    public void sendMail(){
        SendMailRequestBuilder builder=new SendMailRequestBuilder();
        IsyscoreHashMap headers = new IsyscoreHashMap();
        headers.put("X-Isyscore-Permission-Sid", "toekn");//添加登录信息
        headers.put("Content-Type","multipart/form-data");//发送邮件需要的发送格式
        builder.setHeaders(headers);
        MailRequestDomain mailRequestDomain=new MailRequestDomain();
        mailRequestDomain.setFrom("XXX");//发件人邮箱
        mailRequestDomain.setTo("XXX");//收件人邮箱
        mailRequestDomain.setToName("XXX"); //收件人名称
        mailRequestDomain.setSubject("XXX");//邮件主题
        mailRequestDomain.setText("XXX");//邮件主要内容
        builder.setBizModel(mailRequestDomain);
        commonService.sendMail(builder);
    }

    /**
     * 查询列表
     * @param pageNum 页数
     * @param pageSize 每页大小
     * @param formId 表单id
     * @param token token
     * @return 由数据总量和当前页数据组成的map
     */
    @GetMapping("/list")
    public @NotNull Map<String,Object> getList(@NotNull @RequestParam("pageNum") int pageNum,@NotNull @RequestParam("pageSize")int pageSize,
                                               @NotNull @RequestParam("formId") String formId,@NotNull @RequestParam("token") String token){

        //构造查询的条件
        FindFormDataListBuilder builder = new FindFormDataListBuilder();
        IsyscoreHashMap headers = new IsyscoreHashMap();
        headers.put("X-Isyscore-Permission-Sid", token);//添加登录信息
        builder.setHeaders(headers);
        PageAndSeachDomain pageAndSeachDomain  = new PageAndSeachDomain();
        pageAndSeachDomain.setPageNum(pageNum); //页数
        pageAndSeachDomain.setPageSize(pageSize);//每页大小
        IsyscoreFormRecordSearchDomain isyscoreFormRecordSearchDomain = new IsyscoreFormRecordSearchDomain();
        isyscoreFormRecordSearchDomain.setFormId(formId); // 表单ID
        pageAndSeachDomain.setSearch(isyscoreFormRecordSearchDomain);
        builder.setRequest(pageAndSeachDomain);

        //调用SDK进行查询
        FindFormDataListResult result = formBizDataService.findFormDataList(builder);


        List<Result> list=new ArrayList<>();
        long total=0;
        if (result.isSuccess()){
            //对查询获得的结果进行处理，转化成我们需要的List<Result>
            List<IsyscoreFormRecordDomain> isyscoreFormRecordDomainList=result.parseData().getPage().getRecords();
            total=result.parseData().getPage().getTotal();
            if (isyscoreFormRecordDomainList!=null){
                for (IsyscoreFormRecordDomain isyscoreFormRecordDomain:isyscoreFormRecordDomainList){
                    Result result1=new Result();
                    result1.setId(isyscoreFormRecordDomain.getId());
                    result1.setFormId(isyscoreFormRecordDomain.getFormId());
                    result1.setRecord(isyscoreFormRecordDomain.getRecordData());
                    list.add(result1);
                }
            }

        }

        //将处理好的结果进行封装，并传回到前端
        Map<String,Object> map=new HashMap<>();
        map.put("total",total);
        map.put("record",list);
        return map;
    }

    /**
     * 增加数据
     * @param record 表单数据
     * @return 是否成功
     */
    @PostMapping("add")
    public boolean addOne(@RequestBody Record record){

        //将接收到的Record对象处理成addOrUpdateFormData函数所需要的参数
        AddOrUpdateFormDataBuilder addFormDataBuilder=new AddOrUpdateFormDataBuilder();
        IsyscoreHashMap headers = new IsyscoreHashMap();
        headers.put("X-Isyscore-Permission-Sid", record.getToken());
        addFormDataBuilder.setHeaders(headers);
        AddOrUpdateFormData addFormData=new AddOrUpdateFormData();
        addFormData.setFormId(record.getFormId());
        addFormData.setAppCode(record.getAppCode());
        addFormData.setRecordData(record.getRecordData());
        List<AddFormDataField> list = new ArrayList<>();
        for (AddFormDataField addFormDataField: record.getFieldIdRecordList()) {
            AddFormDataField addFormDataField1=new AddFormDataField();
            addFormDataField1.setFieldId(addFormDataField.getFieldId());
            addFormDataField1.setFieldCode(addFormDataField.getFieldCode());
            addFormDataField1.setRecord(addFormDataField.getRecord());
            list.add(addFormDataField1);
        }
        addFormData.setFieldIdRecordList(list);
        addFormDataBuilder.setAddOrUpdateFormData(addFormData);

        //自定义特殊逻辑，发送短信或邮件
        //sendMail();
        //sendMessage("12345678987","token");

        //调用SDK进行增加
        AddOrUpdateFormDataResult result=formBizDataService.addOrUpdateFormData(addFormDataBuilder);

        return true;
    }
}
